package cn.edu.csust.cvs.management.support;

import cn.edu.csust.cvs.management.utils.HttpClientResult;
import cn.edu.csust.cvs.management.utils.HttpClientUtils;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class Speech2TextSupport {


    @Value("${local.path.audio}")
    String audioPath;

    private static final String STTURL = "http://recognition:8000/speech/to/text";
    private static final String TTSURL = "http://synthesis:8000/text/to/speech";
    private static final String PUNCURL = "http://text2punctuation:8000/text/to/punctuation";
    private static final String STANDARD = "/data/files/audios/standard.wav";

    /**
     * 语音识别
     * @param audioPath
     * @return
     */
    public String s2t(String audioPath){
        HttpClientResult result = null;
        try {
            Map<String, String> params = new HashMap<>();
            params.put("file_path",audioPath);
            result = HttpClientUtils.doGet(STTURL,params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result != null ? result.getContent() : null;
    }

    /**
     * 语音合成
     * @param text
     * @return
     */
    public List<String> t2s(String text){
        HttpClientResult result;
        try {
            Map<String, String> params = new HashMap<>();
            params.put("wav_path",STANDARD);
            params.put("text",text);
            result = HttpClientUtils.doGet(TTSURL,params);
            List<String> paths = JSONArray.parseArray(result.getContent(),String.class);
            return paths;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文本加注标点
     * @param text
     * @return
     */
    public String punctuation(String text){
        HttpClientResult result;
        try {
            Map<String, String> params = new HashMap<>();
            params.put("text",text);
            result = HttpClientUtils.doGet(PUNCURL,params);
            return result.getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
